package com.one.material.controller;


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.one.material.entity.Material;
import com.one.material.service.IMaterialService;
import com.one.material.struct.PageResult;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liZhenHua
 * @since 2021-12-08
 */
@RestController
@CrossOrigin
@RequestMapping("material")
public class MaterialController {

    @Resource
    IMaterialService materialService;

    /**
     *  获取到所有的物料信息
     * @return List<Material>
     */
    @RequestMapping("getMaterialAll")
    @HystrixCommand(fallbackMethod = "getMethodTo")
    public List<Material> getMaterialAll() {
        return materialService.list();
    }

    /**
     * 根据物料id查询具体物料信息
     * @param id 物料id
     * @return 返回Material对象
     */
    @RequestMapping("getMaterialById")
    public Material getMaterialById(@RequestBody Integer id){

        return materialService.getById(id);
    }


    /**
     *  熔断
     * @return  List<Material>
     */
    public List<Material> getMethodTo() {
        List<Material> list = materialService.list();
                    list.add(new Material().setName("执行的熔断"));
                    //TODO
        return list;
    }

    /**
     *
     * @param material 模糊查询
     * @param pageNum 起始页
     * @param pageSize 每页显示条数
     * @return PageResult<Material>
     */
    @RequestMapping("getMaterialPageAll/{pageNum}/{pageSize}")
    public PageResult<Material> getMaterialPageAll(@RequestBody Material material,
                                                   @PathVariable("pageNum") Integer pageNum,
                                                   @PathVariable("pageSize") Integer pageSize) {
        System.out.println(material);
        return materialService.getMaterialPageAll(material, pageNum, pageSize);
    }


    /**
     * 添加修改物料信息
     * @param material 物料基本信息
     * @return boolean
     */
    @RequestMapping("addOrUpdateMaterial")
    public boolean addOrUpdateMaterial(@RequestBody Material material) {
        if (StringUtils.isEmpty(material.getId())){
            material.setUpdateTime(new Date());
        }
        return materialService.saveOrUpdate(material);
    }

    /**
     *  根据id进行删除物料
     * @param id 物料id
     * @return boolean
     */
    @RequestMapping("delMaterial/{id}")
    public  boolean delMaterial(@PathVariable("id")Integer id){
        return materialService.removeById(id);
    }

    /**
     * 添加图片
     * @param file 文件
     * @return 图片名
     * @throws IOException 添加文件异常
     */
    @RequestMapping("addImg")
    public  String  addImg(MultipartFile file) throws IOException {
        if (file!=null&&!file.isEmpty()){
            String filename = file.getOriginalFilename();
            assert filename != null;
            String imgName= System.currentTimeMillis()+filename.substring(filename.lastIndexOf("."));
            File file1 = new File("d:/img", imgName);
            file.transferTo(file1);
            return imgName;
        }
        return null;
    }

}
